package com.yxwl.demo.ui.scan;

import com.print.base.bean.DeviceItem;
import com.print.base.bean.PrinterConstantPool;
import com.print.base.utils.BluetoothUtil;
import com.print.printer.PrinterManage;
import com.yxwl.demo.ui.adapter.BlueDeviceViewAdapter;

import java.util.List;


public class ScanSPP extends BaseScan {
    protected BluetoothUtil bluetoothUtil;

    @Override
    protected void init() {
        super.init();
        model = PrinterConstantPool.SocketType.SPP;
    }

    @Override
    protected void afterInit() {
        BluetoothUtil.BluetoothCallback bluetoothCallback = (status, msg) -> {
            if (status == 0) {
                scan();
            }
        };
        bluetoothUtil = new BluetoothUtil();
        bluetoothUtil.init(this, bluetoothCallback);
        bluetoothUtil.checkPermission();
    }

    protected void initAdapter() {
        adapter = new BlueDeviceViewAdapter(this);
    }



    @Override
    protected void scan() {
        if (!bluetoothUtil.check() ) {
            return;
        }
        addBondedDevices();
    }

    protected void addBondedDevices() {
        List<DeviceItem> devices = PrinterManage.getInstance().getBondedDevices();
        if (devices == null) {
            return;
        }
        for (DeviceItem deviceItem : devices) {
            addDevice(deviceItem);
        }


        titleTv.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (scanner!=null){
                    scanner.scan();
                }
            }
        },500);
    }

}
